package com.cl.short_video_java.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.short_video_java.common.Const;
import com.cl.short_video_java.exception.WebException;
import com.cl.short_video_java.mapper.SysRoleMenuMapper;
import com.cl.short_video_java.pojo.SysMenu;
import com.cl.short_video_java.pojo.SysRoleMenu;
import com.cl.short_video_java.pojo.VideoRole;
import com.cl.short_video_java.pojo.VideoUser;
import com.cl.short_video_java.service.SysMenuService;
import com.cl.short_video_java.mapper.SysMenuMapper;
import com.cl.short_video_java.service.VideoRoleService;
import com.cl.short_video_java.service.VideoUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author object
 * @description 针对表【sys_menu(菜单权限表)】的数据库操作Service实现
 * @createDate 2024-10-31 15:58:59
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
		implements SysMenuService {

	private VideoUserService videoUserService;
	private VideoRoleService videoRoleService;
	private SysRoleMenuMapper sysRoleMenuMapper;

	@Autowired
	SysMenuServiceImpl(VideoUserService videoUserService, VideoRoleService videoRoleService,
					   SysRoleMenuMapper sysRoleMenuMapper) {
		this.videoUserService = videoUserService;
		this.videoRoleService = videoRoleService;
		this.sysRoleMenuMapper = sysRoleMenuMapper;
	}

	@Override
	public List<SysMenu> roleMenu(HttpServletRequest request) {
		VideoUser user = videoUserService.getCurrentUser(request, Const.GRADE_ADMIN);

		if (user.getId() == null) {
			throw new WebException("用户不存在", 401);
		}
		List<SysMenu> list = new ArrayList<>();

		Integer roleId = user.getRoleId();
		VideoRole videoRole = videoRoleService.getById(roleId);
		System.out.println("获取的角色信息====================================" + videoRole);
		if ("admin".equals(videoRole.getRoleKey())) {
			// 超级管理员
			// 导航页面
			list = this.list(new QueryWrapper<SysMenu>()
					.select("menu_id", "menu_name", "parent_id", "order_num", "path", "component", "route_name", "icon", "visible")
					.eq("order_num", 0));
			for (SysMenu sysMenu : list) {
				List<SysMenu> cMenu = this.list(new QueryWrapper<SysMenu>()
						.select("menu_id", "menu_name", "parent_id", "order_num", "path", "component", "route_name", "icon", "visible")
						.eq("order_num", 1).eq("parent_id", sysMenu.getMenuId()));
				sysMenu.setChildren(cMenu);
			}
		} else {
			List<SysRoleMenu> sysRoleMenus = sysRoleMenuMapper.selectList(new QueryWrapper<SysRoleMenu>()
					.eq("role_id", videoRole.getRoleId())
					.eq("status", 1));
			List<Long> menuIds = sysRoleMenus.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
			list = this.list(new QueryWrapper<SysMenu>()
					.select("menu_id", "menu_name", "parent_id", "order_num", "path", "component", "route_name", "icon", "visible")
					.in("menu_id", menuIds)
					.eq("order_num", 0));
			for (SysMenu sysMenu : list) {
				List<SysMenu> cMenu = this.list(new QueryWrapper<SysMenu>()
						.select("menu_id", "menu_name", "parent_id", "order_num", "path", "component", "route_name", "icon", "visible")
						.eq("order_num", 1).eq("parent_id", sysMenu.getMenuId())
						.in("menu_id", menuIds)
				);
				sysMenu.setChildren(cMenu);
			}
		}

		return list;
	}

	@Override
	public List<SysMenu> selectAll() {
		// 获取超父级菜单
		List<SysMenu> list = this.list(new QueryWrapper<SysMenu>().select("menu_id", "menu_name")
				.eq("order_num", 0));
		for (SysMenu sysMenu : list) {
			// 获取子级菜单
			List<SysMenu> list1 = this.list(new QueryWrapper<SysMenu>().select("menu_id", "menu_name")
					.eq("order_num", 1).eq("parent_id", sysMenu.getMenuId()));
			sysMenu.setChildren(list1);
			for (SysMenu sysMenu1 : list1) {
				List<SysMenu> list2 = this.list(new QueryWrapper<SysMenu>().select("menu_id", "menu_name")
						.eq("order_num", 2).eq("parent_id", sysMenu1.getMenuId()));
				sysMenu1.setChildren(list2);
			}
		}


		return list;
	}
}




